package hexui;

import hexgame.HexGame;
import hexgame.PlayMethods;

import java.util.ArrayList;
import java.util.List;
import java.util.TimerTask;

import javax.swing.JOptionPane;

import program.GameManager;

public class CheckForEndedGames extends TimerTask {
	@Override
	public void run() {
		List<HexGame> games = GameManager.getGames();
		ArrayList<HexGame> endedGames = new ArrayList<HexGame>();
		for (HexGame game : games) {
			if(game.hasEnded()){
				String message = "Server hosted " + PlayMethods.getPlayerFactoryName(game.getRequest().localPlayerFactory);
				if (game.hasLocalPlayerWon()) {
					message += " has won.";
				} else {
					message += " has lost.";
				}
				JOptionPane.showMessageDialog(null, message);
				endedGames.add(game);
			}
		}
		if (endedGames.size() > 0) {
			for (HexGame game : endedGames) {
				games.remove(game);
			}
		}
		GameManager.setGames(games);
		endedGames.clear();
	}
}

